<template>
  <a-drawer
    :visible="formVisible"
    :width="500"
    :title="isEdit ? '编辑' : '新增'"
    @ok="submit"
    @cancel="close"
  >
    <div>
      <a-form
        ref="formRef"
        :model="form"
        :rules="rules"
        :label-col-props="{ span: 8 }"
        :wrapper-col-props="{ span: 16 }"
      >
        <a-form-item field="nickName" label="昵称">
          <a-input v-model="form.nickName" placeholder="请填写昵称" />
        </a-form-item>
        <a-form-item field="headimgUrl" label="头像">
          <a-input v-model="form.headimgUrl" placeholder="请填写头像" />
        </a-form-item>
        <a-form-item field="sex" label="性别">
          <a-input v-model="form.sex" placeholder="请填写性别" />
        </a-form-item>
        <a-form-item field="phone" label="手机号码">
          <a-input v-model="form.phone" placeholder="请填写手机号码" />
        </a-form-item>
        <a-form-item field="points" label="当前积分">
          <a-input v-model="form.points" placeholder="请填写当前积分" />
        </a-form-item>
        <a-form-item field="referId" label="推荐人">
          <a-input v-model="form.referId" placeholder="请填写推荐人" />
        </a-form-item>
        <a-form-item field="userGrade" label="用户等级">
          <a-input v-model="form.userGrade" placeholder="请填写用户等级" />
        </a-form-item>
      </a-form>
    </div>
  </a-drawer>
</template>

<script lang="ts" setup>
  import { Message } from '@arco-design/web-vue';
  import { computed, ref } from 'vue';
  import { useVisible } from '@/hooks/visible';
  import { addUserInfo, updateUserInfo } from '@/api/mall/userinfo';

  const formRef = ref();

  const initData = () => {
    return {
      createTime: '',
      headimgUrl: '',
      id: '',
      nickName: '',
      passwd: '',
      phone: '',
      points: '',
      referId: '',
      sex: '',
      updateTime: '',
      userGrade: '',
    };
  };

  const rules = {
    createTime: [
      {
        required: true,
        message: '创建时间不能为空',
      },
    ],

    id: [
      {
        required: true,
        message: 'PK不能为空',
      },
    ],

    updateTime: [
      {
        required: true,
        message: '更新时间不能为空',
      },
    ],
  };

  const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
    },
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  });

  const emit = defineEmits<{
    (e: 'update:visible', v: boolean): void;
    (e: 'refresh'): void;
  }>();

  const form = ref(props.formData);

  const { formVisible, close, open } = useVisible(props, emit);

  // 判断是编辑还是新增
  const isEdit = computed(() => !!form.value.id);

  const submit = async () => {
    formVisible.value = true;
    const validate = await formRef.value.validate();
    if (!validate) {
      if (isEdit.value) {
        await updateUserInfo(form.value).then(() => {
          Message.success('修改成功');
        });
      } else {
        await addUserInfo(form.value).then(() => {
          Message.success('添加成功');
        });
      }
      form.value = { ...initData };
      formVisible.value = false;
      emit('refresh');
    }
  };

  open(() => {
    const { id } = props.formData;
    if (id) {
      form.value = props.formData;
    } else {
      form.value = initData();
    }
  });
</script>
